/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ErrCorrect.java
 *
 * Created on 12.4.2010, 11:08:
 */

package com.example;

import javax.swing.JOptionPane;

/**
 *
 * @author Solev
 */
public class ErrCorrect extends javax.swing.JFrame {

    private String vlez;
    /** Creates new form ErrCorrect */
    public ErrCorrect(String err) {
        vlez = err;
        initComponents();
        obrabotiGreshka(vlez);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txaResult = new javax.swing.JTextArea();
        txtWrong = new javax.swing.JTextField();
        txtRight = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        btnFix = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("There were errors in your text. Some structures were not in ACE!");

        txaResult.setColumns(20);
        txaResult.setRows(5);
        jScrollPane1.setViewportView(txaResult);

        jLabel2.setText("Fix:");

        jLabel3.setText("To:");

        btnFix.setText("Fix error");
        btnFix.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFixActionPerformed(evt);
            }
        });

        jLabel4.setText("To fix multiple errors at once, separate the words in the textboxes with semicolons.");

        jLabel5.setText("If you wish to fix the errors inside the document, click on the close (X) button.");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(43, 43, 43))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 401, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtWrong, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtRight, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnFix)))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addContainerGap(37, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtWrong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnFix)
                    .addComponent(txtRight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel4.getAccessibleContext().setAccessibleName("To fix multiple errors at once, separate the words in the textboxes with semicolons.");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnFixActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFixActionPerformed
        // TODO add your handling code here:
        String error = txtWrong.getText().toString();
        error = error.trim();
        String [] errors = error.split(";");
        for(int i = 0; i < errors.length; i++) errors[i] = errors[i].trim();
        String correct = txtRight.getText().toString();
        correct = correct.trim();
        String [] corrects = correct.split(";");
        for(int i = 0; i < errors.length; i++) corrects[i] = corrects[i].trim();
        String text = ProbaAddOn3.xText.getString();
        if(errors.length != corrects.length) {
            JOptionPane.showMessageDialog(null, "The count of the errors is not the same as the count of the corrections", "Message", JOptionPane.INFORMATION_MESSAGE);
        }
        else {
            for(int i = 0; i < corrects.length; i++)
                text = text.replaceAll(errors[i], corrects[i]);

        }
        
        //text = text.replaceAll(error, correct);
        ProbaAddOn3.xText.setString(text);
        ProbaAddOn3.doWork(ProbaAddOn3.command, ProbaAddOn3.web);
        this.dispose();
    }//GEN-LAST:event_btnFixActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnFix;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea txaResult;
    private javax.swing.JTextField txtRight;
    private javax.swing.JTextField txtWrong;
    // End of variables declaration//GEN-END:variables

    protected void obrabotiGreshka (String err) {

        err = err.replaceAll("<messages>", ""); //chistenje na tagovite za poraki
        err = err.replaceAll("</messages>","");
        err = err.replaceAll("/>", "");
        String[] sodrzina = err.split("<message "); //delenje kaj tagot za sekoja poedinechna poraka
                                                    //gi izdvojuvame site poraki
        Integer a = sodrzina.length;

        for(int i = 0; i < sodrzina.length; i++)
            sodrzina[i] = sodrzina[i].trim();       //trganje na praznite mesta

        //se pochnuva od sodrzina[1] bidejkji sodrzina[0] posle delenjeto ne sodrzhi nishto osven prazni mesta
        for(Integer i = 1; i < sodrzina.length; i++) {

            txaResult.append("Message No. " + i.toString() + ":\n");
            String [] poraka = sodrzina[i].split("\n");

            for (int j = 0; j < poraka.length; j++)
                poraka[j] = poraka[j].trim();

            for(int j = 0; j < poraka.length; j++)
                txaResult.append(poraka[j]+"\n");
            txaResult.append("\n");
        }
    }
}
